<?php

namespace App\Http\Controllers;

use App\Models\Rooms;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class RoomsController extends BaseController
{
    public  function index(Request $request)
    {
        $room_num = $request->get("room_num");
        $room_name = $request->get("room_name");
        $floor = $request->get("floor");
        $status = $request->get("status");
        $page_size = $request->get("page_size",10);
        $page_num = $request->get("page_num",1);
        $where = [];

        if ($room_num) {
            $where[] = ["room_num", $room_num];
        }

        if ($room_name) {
            $where[] = ["date", "like", "%".$room_name."%"];
        }

        if ($floor) {
            $where[] = ["floor", $floor];
        }
        if ($status) {
            $where[] = ["status",$status];
        }

        $roomList = Rooms::where($where)
            ->orderBy("updated_at", "desc")
            ->paginate($page_size, ['*'], "page", $page_num);
        $list = $roomList->getCollection();
        return Success(10100, [
            "data" => $list,
            "page_info" => [
                'current' => $roomList->currentPage(), 'page_size' => (int)$page_size, "count" => $roomList->total(),
                "pages" => $roomList->lastPage()
            ]
        ]);

    }

    /**
     * 房间新增
     * @param Request $request
     * @return JsonResponse
     */
    public  function store(Request $request): JsonResponse
    {
        $room_num = $request->get("room_num");
        $room_name = $request->get("room_name");
        $floor = $request->get("floor");
        $wifi_name = $request->get("wifi_name");
        $wifi_pass = $request->get("wifi_pass");
        $desc = $request->get("desc");
        if(!$room_num || !$room_name || !$floor || !$wifi_name || !$wifi_pass){
            return Failed(10002, ["file" => "RoomsController", "line" => __LINE__, "need_filed" => ['room_num', 'room_name', 'floor', 'wifi_name','wifi_pass',]]);
        }
        $room_info = Rooms::where([['room_num',$room_num]])->first();
        if($room_info ){
            return $this->fail(10002, "房间号已存在,请更换", ["file" => "GoodsController", "line" => __LINE__, "room_info" => $room_info]);
        }
        $room = new Rooms();
        $room->room_name = $room_name;
        $room->room_num = $room_num;
        $room->floor = $floor;
        $room->wifi_name = $wifi_name;
        $room->wifi_pass = $wifi_pass;
        $room->desc = $desc;
        $room->updated_at = date("Y-m-d H:i:s");
        $room->created_at = date("Y-m-d H:i:s");
        if(!$room->save()){
            return $this->fail(10002, "房间添加失败", ["file" => "GoodsController", "line" => __LINE__, "room_info" => $room_info]);
        }
        return Success(10000, []);
    }

    public  function show()
    {

    }


    public  function update()
    {

    }

    public function destroy()
    {

    }
}
